package com.lvmama.rhino.web;

import com.google.common.collect.Maps;
import com.lvmama.hippo.api.service.HBaseTableService;
import com.lvmama.rhino.entity.DeviceInfoQueryVO;
import com.lvmama.rhino.utils.HBaseKeyUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

/**
 * @author wuchuantong
 * @version V1.0
 * @Title: DeviceInfoQueryController
 * @Package com.lvmama.rhino.web
 * @Description: 设备信息查询实现类
 * @date 2017/3/22 15:15
 */
@Controller
@RequestMapping("deviceInfoQuery")
public class DeviceInfoQueryController {

    protected Logger logger = Logger.getLogger(this.getClass());


    @Autowired
    private HBaseTableService hBaseTableService;

    @RequiresPermissions("sys:deviceInfoQuery:view")
    @RequestMapping("list")
    public String list(DeviceInfoQueryVO deviceInfoQueryVO, Model model) {
        logger.info("deviceToken :" + deviceInfoQueryVO.getDeviceToken());
        if (StringUtils.isNotBlank(deviceInfoQueryVO.getDeviceToken())) {
            String rowKey = HBaseKeyUtils.getDeviceRowKey(deviceInfoQueryVO.getDeviceToken());
            Integer maxVersions = 10;

            Map<String, List<List<String>>> mapList = hBaseTableService.findByRowKeyList("push_deviceInfo", "baseInfo", rowKey, maxVersions);

            if (MapUtils.isNotEmpty(mapList)) {
                //将 Map<String, Map<String, String>>  转成List<List<String>>的形式
                Set<String> titles = mapList.keySet();
                logger.info("title size:" + titles.size());
                List<List<String>> resultList = new ArrayList<>(maxVersions);
                for (int i = 0; i < maxVersions; i++) {
                    List<String> valueList = new ArrayList<>();
                    for (String title : titles) {
                        valueList.add("");
                        valueList.add("");
                    }
                    resultList.add(valueList);
                }
                int colIdx = 0;
                for (Map.Entry<String, List<List<String>>> mapEntry : mapList.entrySet()) {
                    int rowIdx = 0;
                    List<List<String>> listVal = mapEntry.getValue();
                    for (List<String> vals : listVal) {
                        resultList.get(rowIdx).set(colIdx * 2, vals.get(0));
                        resultList.get(rowIdx).set(colIdx * 2 + 1, vals.get(1));
                        rowIdx++;
                    }
                    colIdx++;
                }
                model.addAttribute("deviceInfos", resultList);
                model.addAttribute("deviceInfoTitles", titles);
            }
        }
        model.addAttribute("conditionVO", deviceInfoQueryVO);
        return "application/pushStatistic/deviceInfoQuery";
    }


    /**
     * 跳转至列表页面
     *
     * @param model 用于放入页面需要的数据
     * @return 跳转页面
     */
    @RequiresPermissions("sys:deviceInfoQuery:view")
    @RequestMapping("toList")
    public String toList(Model model) {
//        putPlatformToModel(model);
        return "application/pushStatistic/deviceInfoQuery";
    }


    public static void main(String[] args) {
    }
}
